package com.ixion.blog.modules;

import java.util.ArrayList;
import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Stopwatch;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.IocBy;
import org.nutz.mvc.annotation.Modules;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.ioc.provider.ComboIocProvider;

import com.assic.utils.db.DBImportor;
import com.assic.utils.vo.Result;
import com.ixion.blog.bean.Article;
import com.ixion.blog.dao.IArticleDao;
import com.ixion.blog.dao.IUserDao;
import com.ixion.blog.modules.common.BaseModule;

@Modules(scanPackage = true)
@IocBy(type = ComboIocProvider.class, args = { "*org.nutz.ioc.loader.json.JsonLoader", "config/blog.json", "*org.nutz.ioc.loader.annotation.AnnotationIocLoader", "com.ixion.blog" })
@At("/")
@Fail("json")
public class MainModule extends BaseModule {

	@Inject
	private Dao dao;
	@Inject
	private IUserDao userDao;
	@Inject
	private IArticleDao articleDao;

	@At("/initDB")
	@Ok("json")
	public Result test() {
		dao.create(Article.class, true);
		// Article article = new Article();
		// article.setTitle("test");
		// article.setContent("test_content");
		// article.setSummary("test_summary");
		// article.setPublishDate(Times.now());
		// articleDao.save(article);
		// List<User> users = new ArrayList<User>();
		// Timer.start();
		// for (int i = 0; i < 10000; i++) {
		// User user = new User();
		// user.setName("User_" + i);
		// users.add(user);
		// }
		// Stopwatch stopwatch = Stopwatch.beginNano();
		// for (User user : users) {
		// Timer.start();
		// userDao.save(user);
		// Timer.end();
		// }
		// stopwatch.stop();
		// System.out.println(stopwatch.getDuration());
		// Timer.end();
		// return Result.success();
		List<String> infos = new ArrayList<String>();
		for (int i = 0; i < 230000; i++) {
			infos.add("INSERT INTO blog_article(article_title,article_summary,article_content,article_publish_date) VALUES('test_" + i + "','test_summary','test_content','2014-05-11 11:36:56')");
		}

		// List<Sql> sqls = new ArrayList<Sql>();
		// for (int i = 0; i < 30000; i++) {
		// sqls.add(Sqls.create("INSERT INTO blog_user(u_name,u_age,u_pwd,u_real_name,u_sex,u_brith,u_home_page,u_email) VALUES('User3m__"
		// + i + "',0,NULL,NULL,0,NULL,NULL,NULL) "));
		// }
		Stopwatch stopwatch = Stopwatch.begin();
		DBImportor.importFromSQLList(infos, dao);
		// dao.execute(Lang.collection2array(sqls));
		stopwatch.stop();
		System.out.println(stopwatch.getDuration());
		return Result.success();

	}
}
